<template>
  <div class="crm-page home-page">
    <div class="page-header">商机详情</div>
    <div class="page-content transparent">
      <div class="page-card">
        <div class="card-title">
          <el-button class="back-btn" size="medium" type="text" icon="el-icon-back" @click="goMainBack()">返回</el-button>
        </div>
        <div class="page-card-content">
          <div class="item-title" style="margin-top:20px">
            基本信息
          </div>
          <div class="card-item">
            <el-row :gutter="10">
              <el-col :xs="24" :sm="12" :md="12" :lg="6" :xl="4">
                <span class="title">商机名称：</span>
                <span class="content">{{ data.name }}</span>
              </el-col>
              <el-col :xs="24" :sm="12" :md="12" :lg="6" :xl="4">
                <span class="title">客户名：</span>
                <span class="content">
                  <span class="app-link" @click="gotoMainPage('customer/info',{id:data.customerId})">  {{ data.customerName }}</span>
                </span>
              </el-col>
              <el-col :xs="24" :sm="12" :md="12" :lg="6" :xl="4">
                <span class="title">结束时间：</span>
                <span class="content">{{ formatTime(data.endDate,'YYYY-MM-DD') }}</span>
              </el-col>
              <el-col :xs="24" :sm="12" :md="12" :lg="6" :xl="4">
                <span class="title">金额（元）：</span>
                <span class="content">{{ data.amount | moneyFormat }}</span>
              </el-col>
              <el-col :xs="24" :sm="12" :md="12" :lg="6" :xl="4">
                <span class="title">所有人：</span>
                <span class="content">{{ data.owner }}</span>
              </el-col>
            </el-row>
          </div>
        </div>
      </div>
      <div class="page-card">
        <ChanceStatusLine v-if="data.saleStage" :status="data.saleStage" @change="handleChangeStatus" />
      </div>
      <el-row :gutter="20">
        <el-col :xs="24" :sm="24" :md="24" :lg="14" :xl="14">
          <div class="page-card">
            <div class="page-card-content">
              <div class="item-title">
                详细信息
              </div>
              <div class="card-item">
                <el-row :gutter="10">
                  <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
                    <span class="title">商机名称：</span>
                    <span class="content">{{ data.name }}</span>
                  </el-col>
                  <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
                    <span class="title">所有人：</span>
                    <span class="content">{{ data.owner }}</span>
                  </el-col>
                  <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
                    <span class="title">客户名：</span>
                    <span class="content">{{ data.customerName }}</span>
                  </el-col>
                  <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
                    <span class="title">结束日期：</span>
                    <span class="content">{{ formatTime(data.endDate,'YYYY-MM-DD') }}</span>
                  </el-col>
                  <!-- <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
                    <span class="title">职务：</span>
                    <span class="content">{{ data.position }}</span>
                  </el-col> -->
                  <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
                    <span class="title">阶段：</span>
                    <span class="content">{{ data.saleStageText }}</span>
                  </el-col>
                  <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
                    <span class="title">类型：</span>
                    <span class="content">{{ data.typeText }}</span>
                  </el-col>
                  <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
                    <span class="title">市场活动：</span>
                    <span class="content">{{ data.marketActivity }}</span>
                  </el-col>
                  <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
                    <span class="title">金额（元）：</span>
                    <span class="content">{{ data.amount | moneyFormat }}</span>
                  </el-col>
                </el-row>
                <el-row :gutter="10">
                  <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
                    <span class="title">备注：</span>
                    <span class="content">{{ data.remark }}</span>
                  </el-col>
                </el-row>
                <el-row :gutter="10">
                  <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
                    <span class="title">创建人：</span>
                    <span class="content">{{ data.createName }}</span>
                  </el-col>
                  <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
                    <span class="title">上次修改人：</span>
                    <span class="content">{{ data.updateName }}</span>
                  </el-col>
                  <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
                    <span class="title">创建时间：</span>
                    <span class="content">{{ formatTime(data.createTime) }}</span>
                  </el-col>
                  <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
                    <span class="title">上次修改时间：</span>
                    <span class="content">{{ formatTime(data.updateTime) }}</span>
                  </el-col>
                </el-row>
              </div>
            </div>
          </div>
          <ContactCard v-if="data.id" :customer-id="data.customerId" :customer-name="data.customerName" />
          <QuoteCard v-if="data.id" :customer-id="data.customerId" :customer-name="data.customerName" />
          <ContractCard v-if="data.id" :customer-id="data.customerId" :customer-name="data.customerName" />

        </el-col>
        <el-col :xs="24" :sm="24" :md="24" :lg="10" :xl="10">
          <ActivityCard
            :data="{
              name:data.customerName,
              relateName:data.name,
              relateId:data.id
            }"
            :type="4"
            @change="handleCardChange"
          />
          <ActivityListCard v-if="data.id" ref="listCard" :relateid="data.id" :type="4" />
        </el-col>

      </el-row>

    </div>
  </div>
</template>
<script>
import ChanceStatusLine from '@/components/ChanceStatusLine'
import ActivityCard from '@/components/ActivityCard'
import ContactCard from '@/components/InfoCard/ContactCard'
import QuoteCard from '@/components/InfoCard/QuoteCard'
import ContractCard from '@/components/InfoCard/ContractCard'
import ActivityListCard from '@/components/ActivityListCard'
import { getChance, updateSaleStage } from '@/api/chance'

import { getEnumText, clueStatus, saleStages, chanceTypes } from '@/utils/enums'
import { formatTime } from '@/utils/tools'
import BigNumber from 'bignumber.js'
export default {
  name: 'Info',
  components: { ChanceStatusLine, ActivityCard, ActivityListCard, QuoteCard, ContactCard, ContractCard },
  data() {
    return {
      data: {

      }

    }
  },
  mounted() {
    const id = this.$route.query.id
    getChance({ id }).then(res => {
      this.data = res
      if (this.data.saleStage) {
        this.data.statusText = getEnumText(clueStatus, this.data.status)
        this.data.saleStageText = getEnumText(saleStages, this.data.saleStage)
        this.data.typeText = getEnumText(chanceTypes, this.data.type)
        // this.data.amount = this.data.amount ? new BigNumber(this.data.amount).toFixed(4) : ''
      }
    })
  },
  methods: {
    formatTime,
    handleChangeStatus(v) {
      const data = {
        id: this.data.id,
        saleStage: v
      }
      updateSaleStage(data).then(res => {
        this.data.saleStage = v
        this.data.saleStageText = getEnumText(clueStatus, this.data.status)
        this.$message({
          type: 'success',
          message: '修改成功'
        })
      }).catch(ex => {
        this.$message({
          type: 'error',
          message: ex.message || '修改失败'
        })
      })
    },
    handleCardChange() {
      this.$refs.listCard.refresh()
    }

  }
}
</script>

